package view;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.cloudgarden.resource.SWTResourceManager;

import configuration.Config;
import configuration.ConfigOperator;
import controller.CommandsExcuter;
import controller.CommandsExcuter.LogInExcutor;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * this class is the installation screen for client that want to use "CleverDropBox"
 */
public class startScreen extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Label Passwordlabel;
	private Link newAccountLink;
	private Label logoLabel;
	private Label incorrectLabel;
	private Button SubmitButton;
	private Text PasswordText;
	private Text UserNameText;
	private Label UserNameLabel;
	private static PrintStream err;
	private static PrintStream out;

	/**
	 * Auto-generated main method to display this 
	 * org.eclipse.swt.widgets.Dialog inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			//redirection of error and output to a log
			err = new PrintStream(new BufferedOutputStream(new FileOutputStream("errLog.txt")));
			out = new PrintStream(new BufferedOutputStream(new FileOutputStream("outLog.txt")));
			System.setOut(out);
			System.setErr(err);

			Config c = ConfigOperator.read("C:\\Program Files\\project\\ClientService\\Config.xml");
			System.out.println("sssssssssssssss");
			new ErrorMess();
			err.close();
			out.close();
			
		} catch (FileNotFoundException e) {
			//open GUI
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			startScreen inst = new startScreen(shell, SWT.NULL);
			inst.open();
			err.close();
			out.close();
		}
	}
	/**
	 * constructor of the class
	 * @param parent
	 * @param style
	 */
	public startScreen(Shell parent, int style) {
		super(parent, style);
	}
	public static void initGUI(){
		startScreen startScreen = new startScreen(new Shell(), SWT.NONE);
		startScreen.open();
	}
	/**
	 * initialize all GUI components
	 */
	public void open() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		try {
			Shell parent = shell/*getParent()*/;
			dialogShell = new Shell(parent);
			dialogShell.setText("Welcome to CleverDropBox!!!");
			//the logo label
			{
				logoLabel = new Label(dialogShell, SWT.NONE);
				FormData logoLabelLData = new FormData();
				logoLabelLData.left =  new FormAttachment(0, 1000, 12);
				logoLabelLData.top =  new FormAttachment(0, 1000, 12);
				logoLabelLData.width = 132;
				logoLabelLData.height = 155;
				logoLabel.setLayoutData(logoLabelLData);
				logoLabel.setText("here we \nneed to \nput logo\n");
				logoLabel.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));
				logoLabel.setLocation(new org.eclipse.swt.graphics.Point(500, 200));
				logoLabel.setImage(SWTResourceManager.getImage("tour6.png"));

			}

			{
				//Register as a resource user - SWTResourceManager will
				//handle the obtaining and disposing of resources
				SWTResourceManager.registerResourceUser(dialogShell);
			}
			// incorrect Label
			{
				incorrectLabel = new Label(dialogShell, SWT.NONE);
				FormData incorrectLabelLData = new FormData();
				incorrectLabelLData.left =  new FormAttachment(0, 1000, 200);
				incorrectLabelLData.top =  new FormAttachment(0, 1000, 88);
				incorrectLabelLData.width = 166;
				incorrectLabelLData.height = 19;
				incorrectLabel.setLayoutData(incorrectLabelLData);
				incorrectLabel.setText("incorrect user name\\password");
				incorrectLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
				incorrectLabel.setVisible(false);
			}
			//link to create new account
			{
				newAccountLink = new Link(dialogShell, SWT.NONE );
				FormData newAccountLinkLData = new FormData();
				newAccountLinkLData.left =  new FormAttachment(0, 1000, 252);
				newAccountLinkLData.top =  new FormAttachment(0, 1000, 127);
				newAccountLinkLData.width = 101;
				newAccountLinkLData.height = 18;
				newAccountLink.setLayoutData(newAccountLinkLData);
				newAccountLink.setText("<a href=\"Create an account\">Create an account</a>");
				newAccountLink.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						newAccountLinkWidgetSelected(evt);
					}
				});
			}
			//button to old user that want to install the application on other computer
			{
				SubmitButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				FormData SubmitButtonLData = new FormData();
				SubmitButtonLData.left =  new FormAttachment(0, 1000, 195);
				SubmitButtonLData.top =  new FormAttachment(0, 1000, 119);
				SubmitButtonLData.width = 51;
				SubmitButtonLData.height = 24;
				SubmitButton.setLayoutData(SubmitButtonLData);
				SubmitButton.setText("Login");
				SubmitButton.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						SubmitButtonWidgetSelected();
					}
				});
				SubmitButton.setEnabled(false);
			}
			//user name text box
			{
				FormData UserNameTextLData = new FormData();
				UserNameTextLData.left =  new FormAttachment(0, 1000, 283);
				UserNameTextLData.top =  new FormAttachment(0, 1000, 30);
				UserNameTextLData.width = 81;
				UserNameTextLData.height = 17;
				UserNameText = new Text(dialogShell, SWT.NONE | SWT.BORDER);
				UserNameText.setLayoutData(UserNameTextLData);
				UserNameText.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						UserNameTextKeyPressed(evt);
					}
				});
				UserNameText.setFocus();
			}
			//user name label
			{
				UserNameLabel = new Label(dialogShell, SWT.NONE);
				FormData UserNameLabelLData = new FormData();
				UserNameLabelLData.left =  new FormAttachment(0, 1000, 195);
				UserNameLabelLData.top =  new FormAttachment(0, 1000, 34);
				UserNameLabelLData.width = 68;
				UserNameLabelLData.height = 14;
				UserNameLabel.setLayoutData(UserNameLabelLData);
				UserNameLabel.setText("User name:");
			}
			//password text box
			{
				FormData PasswordTextLData = new FormData();
				PasswordTextLData.left =  new FormAttachment(0, 1000, 283);
				PasswordTextLData.top =  new FormAttachment(0, 1000, 65);
				PasswordTextLData.width = 80;
				PasswordTextLData.height = 14;
				PasswordText = new Text(dialogShell, SWT.BORDER | SWT.PASSWORD);
				PasswordText.setLayoutData(PasswordTextLData);
				PasswordText.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						PasswordTextKeyPressed(evt);
					}
				});
			}
			//pasword label
			{
				Passwordlabel = new Label(dialogShell, SWT.NONE);
				FormData PasswordLData = new FormData();
				PasswordLData.left =  new FormAttachment(0, 1000, 200);
				PasswordLData.top =  new FormAttachment(0, 1000, 68);
				PasswordLData.width = 62;
				PasswordLData.height = 16;
				Passwordlabel.setLayoutData(PasswordLData);
				Passwordlabel.setText("Password:");
			}
			//shell
			dialogShell.setLayout(new FormLayout());
			dialogShell.setSize(406, 259);
			dialogShell.layout();
			dialogShell.pack();			
			dialogShell.setSize(406, 217);
			dialogShell.setMaximized(true);
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			//Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*
	 * enter clicked on user name text box
	 */
	private void UserNameTextKeyPressed(KeyEvent evt) {
		if(PasswordText.getText()!=""){
			SubmitButton.setEnabled(true);
			if(evt.keyCode==13)
				SubmitButtonWidgetSelected();
		}
	}
	/*
	 * enter clicked on password text box
	 */
	private void PasswordTextKeyPressed(KeyEvent evt) {
		if(UserNameText.getText()!=""){
			SubmitButton.setEnabled(true);
			if(evt.keyCode==13)
				SubmitButtonWidgetSelected();
		}
	}
	//submit button was selected
	private void SubmitButtonWidgetSelected() {
		//try to match the data to exist user
		LogInExcutor log=CommandsExcuter.getCommand().getLogin();
		log.setUserName(UserNameText.getText());
		log.setPassword(PasswordText.getText());
		log.excute();
		//user exist
		if(log.getReturnVal()){
			//moving to configuration screen
			/*for (Control kid : dialogShell.getChildren()) 
		          kid.dispose();
			computerConfigScreen.showGUI(false,dialogShell);*/
			dialogShell.dispose();
			new InstallWizard(false);


		}
		//user dosn't exist
		else
			incorrectLabel.setVisible(true);
	}
	/*
	 * new account link was clicked 
	 */
	private void newAccountLinkWidgetSelected(SelectionEvent evt) {
		//moving to new account screen
		/*for (Control kid : dialogShell.getChildren()) 
	          kid.dispose();
		createAccountGUI.showGUI(dialogShell);*/
		dialogShell.dispose();
		new InstallWizard(true);

	}

}
